/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (info@cloveretl.com)
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jetel.component.rollup;

import java.util.Properties;

import org.jetel.component.TransformUtils;
import org.jetel.ctl.CTLAbstractTransform;
import org.jetel.ctl.CTLEntryPoint;
import org.jetel.ctl.TransformLangExecutorRuntimeException;
import org.jetel.data.DataRecord;
import org.jetel.exception.ComponentNotReadyException;
import org.jetel.exception.TransformException;
import org.jetel.metadata.DataRecordMetadata;
import org.jetel.util.ExceptionUtils;

/**
 * Base class of all Java transforms generated by CTL-to-Java compiler from CTL transforms in the Rollup component.
 *
 * @author Martin Janik, Javlin a.s. &lt;martin.janik@javlin.eu&gt;
 *
 * @version 23rd June 2010
 * @created 22nd April 2010
 *
 * @see RecordRollup
 */
public abstract class CTLRecordRollup extends CTLAbstractTransform implements RecordRollup {

    /** the name of the initGroup() function in CTL */
    public static final String INIT_GROUP_FUNCTION_NAME = "initGroup";
    /** the name of the initGroupOnError() function in CTL */
    public static final String INIT_GROUP_ON_ERROR_FUNCTION_NAME = "initGroupOnError";
    /** the name of the updateGroup() function in CTL */
    public static final String UPDATE_GROUP_FUNCTION_NAME = "updateGroup";
    /** the name of the updateGroupOnError() function in CTL */
    public static final String UPDATE_GROUP_ON_ERROR_FUNCTION_NAME = "updateGroupOnError";
    /** the name of the finishGroup() function in CTL */
    public static final String FINISH_GROUP_FUNCTION_NAME = "finishGroup";
    /** the name of the finishGroupOnError() function in CTL */
    public static final String FINISH_GROUP_ON_ERROR_FUNCTION_NAME = "finishGroupOnError";
    /** the name of the updateTransform() function in CTL */
    public static final String UPDATE_TRANSFORM_FUNCTION_NAME = "updateTransform";
    /** the name of the updateTransformOnError() function in CTL */
    public static final String UPDATE_TRANSFORM_ON_ERROR_FUNCTION_NAME = "updateTransformOnError";
    /** the name of the transform() function in CTL */
    public static final String TRANSFORM_FUNCTION_NAME = "transform";
    /** the name of the transformOnError() function in CTL */
    public static final String TRANSFORM_ON_ERROR_FUNCTION_NAME = "transformOnError";

    /** the name of the counter param used in CTL */
    public static final String COUNTER_PARAM_NAME = "counter";
    /** the name of the groupAccumulator param used in CTL */
    public static final String GROUP_ACCUMULATOR_PARAM_NAME = "groupAccumulator";

	/** Input data record used by rollup transform, or <code>null</code> if not accessible. */
	private DataRecord inputRecord = null;
	/** Output data records used by rollup transform, or <code>null</code> if not accessible. */
	private DataRecord[] outputRecords = null;

	@Override
	public final void init(Properties parameters, DataRecordMetadata inputMetadata,
			DataRecordMetadata accumulatorMetadata, DataRecordMetadata[] outputMetadata)
			throws ComponentNotReadyException {
		globalScopeInit();
		initDelegate();
	}

	/**
	 * Called by {@link #init(Properties, DataRecordMetadata, DataRecordMetadata, DataRecordMetadata[])} to perform
	 * user-specific initialization defined in the CTL transform. The default implementation does nothing, may be
	 * overridden by the generated transform class.
	 *
	 * @throws ComponentNotReadyException if the initialization fails
	 */
	@CTLEntryPoint(name = INIT_FUNCTION_NAME, required = false)
	protected void initDelegate() throws ComponentNotReadyException {
		// does nothing by default, may be overridden by generated transform classes
	}

	@Override
	public final void initGroup(DataRecord inputRecord, DataRecord groupAccumulator) throws TransformException {
		// only input record is accessible within the initGroup() function
		this.inputRecord = inputRecord;

		try {
			initGroupDelegate(groupAccumulator);
		} catch (ComponentNotReadyException exception) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", exception);
		}

		// make the input record inaccessible again
		this.inputRecord = null;
	}

	/**
	 * Called by {@link #initGroup(DataRecord, DataRecord)} to init processing of a group of data records in a
	 * user-specific way defined in the CTL transform. Has to be overridden by the generated transform class.
	 *
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
     *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = INIT_GROUP_FUNCTION_NAME, parameterNames = {
			GROUP_ACCUMULATOR_PARAM_NAME }, required = true)
	protected abstract void initGroupDelegate(DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException;

	@Override
	public void initGroupOnError(Exception exception, DataRecord inputRecord, DataRecord groupAccumulator)
			throws TransformException {
		// only input record is accessible within the initGroupOnError() function
		this.inputRecord = inputRecord;

		try {
			initGroupOnErrorDelegate(TransformUtils.getMessage(exception), ExceptionUtils.stackTraceToString(exception), groupAccumulator);
		} catch (UnsupportedOperationException ex) {
			// no custom error handling implemented, throw an exception so the transformation fails
			throw new TransformException("Rollup failed!", exception);
		} catch (ComponentNotReadyException ex) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", ex);
		}

		// make the input record inaccessible again
		this.inputRecord = null;
	}

	/**
	 * Called by {@link #initGroupOnError(Exception, DataRecord, DataRecord)} to init processing of a group of data
	 * records in a user-specific way defined in the CTL transform. May be overridden by the generated transform class.
	 * Throws <code>UnsupportedOperationException</code> by default.
	 *
	 * @param errorMessage an error message of the error message that occurred
	 * @param stackTrace a stack trace of the error message that occurred
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = INIT_GROUP_ON_ERROR_FUNCTION_NAME, parameterNames = {
			ERROR_MESSAGE_PARAM_NAME, STACK_TRACE_PARAM_NAME,
			GROUP_ACCUMULATOR_PARAM_NAME }, required = false)
	protected void initGroupOnErrorDelegate(String errorMessage, String stackTrace, DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException {
		throw new UnsupportedOperationException();
	}

	@Override
	public final boolean updateGroup(DataRecord inputRecord, DataRecord groupAccumulator) throws TransformException {
		boolean result = false;

		// only input record is accessible within the updateGroup() function
		this.inputRecord = inputRecord;

		try {
			result = updateGroupDelegate(groupAccumulator);
		} catch (ComponentNotReadyException exception) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", exception);
		}

		// make the input record inaccessible again
		this.inputRecord = null;

		return result;
	}

	/**
	 * Called by {@link #updateGroup(DataRecord, DataRecord)} to update processing of a group of data records in a
	 * user-specific way defined in the CTL transform. Has to be overridden by the generated transform class.
	 *
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = UPDATE_GROUP_FUNCTION_NAME, parameterNames = {
			GROUP_ACCUMULATOR_PARAM_NAME }, required = true)
	protected abstract Boolean updateGroupDelegate(DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException;

	@Override
	public boolean updateGroupOnError(Exception exception, DataRecord inputRecord, DataRecord groupAccumulator)
			throws TransformException {
		boolean result = false;

		// only input record is accessible within the updateGroupOnError() function
		this.inputRecord = inputRecord;

		try {
			result = updateGroupOnErrorDelegate(TransformUtils.getMessage(exception),
					ExceptionUtils.stackTraceToString(exception), groupAccumulator);
		} catch (UnsupportedOperationException ex) {
			// no custom error handling implemented, throw an exception so the transformation fails
			throw new TransformException("Rollup failed!", exception);
		} catch (ComponentNotReadyException ex) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", ex);
		}

		// make the input record inaccessible again
		this.inputRecord = null;

		return result;
	}

	/**
	 * Called by {@link #updateGroupOnError(Exception, DataRecord, DataRecord)} to update processing of a group of data
	 * records in a user-specific way defined in the CTL transform. May be overridden by the generated transform class.
	 * Throws <code>UnsupportedOperationException</code> by default.
	 *
	 * @param errorMessage an error message of the error message that occurred
	 * @param stackTrace a stack trace of the error message that occurred
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = UPDATE_GROUP_ON_ERROR_FUNCTION_NAME, parameterNames = {
			ERROR_MESSAGE_PARAM_NAME, STACK_TRACE_PARAM_NAME,
			GROUP_ACCUMULATOR_PARAM_NAME }, required = false)
	protected Boolean updateGroupOnErrorDelegate(String errorMessage, String stackTrace, DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException {
		throw new UnsupportedOperationException();
	}

	@Override
	public final boolean finishGroup(DataRecord inputRecord, DataRecord groupAccumulator) throws TransformException {
		boolean result = false;

		// only input record is accessible within the finishGroup() function
		this.inputRecord = inputRecord;

		try {
			result = finishGroupDelegate(groupAccumulator);
		} catch (ComponentNotReadyException exception) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", exception);
		}

		// make the input record inaccessible again
		this.inputRecord = null;

		return result;
	}

	/**
	 * Called by {@link #finishGroup(DataRecord, DataRecord)} to finish processing of a group of data records in a
	 * user-specific way defined in the CTL transform. Has to be overridden by the generated transform class.
	 *
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = FINISH_GROUP_FUNCTION_NAME, parameterNames = {
			GROUP_ACCUMULATOR_PARAM_NAME }, required = true)
	protected abstract Boolean finishGroupDelegate(DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException;

	@Override
	public boolean finishGroupOnError(Exception exception, DataRecord inputRecord, DataRecord groupAccumulator)
			throws TransformException {
		boolean result = false;

		// only input record is accessible within the finishGroupOnError() function
		this.inputRecord = inputRecord;

		try {
			result = finishGroupOnErrorDelegate(TransformUtils.getMessage(exception),
					ExceptionUtils.stackTraceToString(exception), groupAccumulator);
		} catch (UnsupportedOperationException ex) {
			// no custom error handling implemented, throw an exception so the transformation fails
			throw new TransformException("Rollup failed!", exception);
		} catch (ComponentNotReadyException ex) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", ex);
		}

		// make the input record inaccessible again
		this.inputRecord = null;

		return result;
	}

	/**
	 * Called by {@link #finishGroupOnError(Exception, DataRecord, DataRecord)} to finish processing of a group of data
	 * records in a user-specific way defined in the CTL transform. May be overridden by the generated transform class.
	 * Throws <code>UnsupportedOperationException</code> by default.
	 *
	 * @param errorMessage an error message of the error message that occurred
	 * @param stackTrace a stack trace of the error message that occurred
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = FINISH_GROUP_ON_ERROR_FUNCTION_NAME, parameterNames = {
			ERROR_MESSAGE_PARAM_NAME, STACK_TRACE_PARAM_NAME,
			GROUP_ACCUMULATOR_PARAM_NAME }, required = false)
	protected Boolean finishGroupOnErrorDelegate(String errorMessage, String stackTrace, DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException {
		throw new UnsupportedOperationException();
	}

	@Override
	public final int updateTransform(int counter, DataRecord inputRecord, DataRecord groupAccumulator,
			DataRecord[] outputRecords) throws TransformException {
		int result = 0;

		// both input and output records are accessible within the updateTransform() function
		this.inputRecord = inputRecord;
		this.outputRecords = outputRecords;

		try {
			result = updateTransformDelegate(counter, groupAccumulator);
		} catch (ComponentNotReadyException exception) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", exception);
		}

		// make both input and output records inaccessible again
		this.inputRecord = null;
		this.outputRecords = null;

		return result;
	}

	/**
	 * Called by {@link #updateTransform(int, DataRecord, DataRecord, DataRecord[])} to transform data records in a
	 * user-specific way defined in the CTL transform. Has to be overridden by the generated transform class.
	 *
     * @param counter the number of previous calls to this method for the current group update
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
     *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = UPDATE_TRANSFORM_FUNCTION_NAME, parameterNames = {
			COUNTER_PARAM_NAME, GROUP_ACCUMULATOR_PARAM_NAME }, required = true)
	protected abstract Integer updateTransformDelegate(Integer counter, DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException;

	@Override
	public int updateTransformOnError(Exception exception, int counter, DataRecord inputRecord,
			DataRecord groupAccumulator, DataRecord[] outputRecords) throws TransformException {
		int result = 0;

		// both input and output records are accessible within the updateTransformOnError() function
		this.inputRecord = inputRecord;
		this.outputRecords = outputRecords;

		try {
			result = updateTransformOnErrorDelegate(TransformUtils.getMessage(exception),
					ExceptionUtils.stackTraceToString(exception), counter, groupAccumulator);
		} catch (UnsupportedOperationException ex) {
			// no custom error handling implemented, throw an exception so the transformation fails
			throw new TransformException("Rollup failed!", exception);
		} catch (ComponentNotReadyException ex) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", ex);
		}

		// make both input and output records inaccessible again
		this.inputRecord = null;
		this.outputRecords = null;

		return result;
	}

	/**
	 * Called by {@link #updateTransformOnError(Exception, int, DataRecord, DataRecord, DataRecord[])} to transform data
	 * records in a user-specific way defined in the CTL transform. May be overridden by the generated transform class.
	 * Throws <code>UnsupportedOperationException</code> by default.
	 *
	 * @param errorMessage an error message of the error message that occurred
	 * @param stackTrace a stack trace of the error message that occurred
     * @param counter the number of previous calls to this method for the current group update
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = UPDATE_TRANSFORM_ON_ERROR_FUNCTION_NAME, parameterNames = {
			ERROR_MESSAGE_PARAM_NAME, STACK_TRACE_PARAM_NAME,
			COUNTER_PARAM_NAME, GROUP_ACCUMULATOR_PARAM_NAME }, required = false)
	protected Integer updateTransformOnErrorDelegate(String errorMessage, String stackTrace, Integer counter,
			DataRecord groupAccumulator) throws ComponentNotReadyException, TransformException {
		throw new UnsupportedOperationException();
	}

	@Override
	public final int transform(int counter, DataRecord inputRecord, DataRecord groupAccumulator,
			DataRecord[] outputRecords) throws TransformException {
		int result = 0;

		// both input and output records are accessible within the transform() function
		this.inputRecord = inputRecord;
		this.outputRecords = outputRecords;

		try {
			result = transformDelegate(counter, groupAccumulator);
		} catch (ComponentNotReadyException exception) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", exception);
		}

		// make both input and output records inaccessible again
		this.inputRecord = null;
		this.outputRecords = null;

		return result;
	}

	/**
	 * Called by {@link #transform(int, DataRecord, DataRecord, DataRecord[])} to transform data records in a
	 * user-specific way defined in the CTL transform. Has to be overridden by the generated transform class.
	 *
     * @param counter the number of previous calls to this method for the current group update
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = TRANSFORM_FUNCTION_NAME, parameterNames = {
			COUNTER_PARAM_NAME, GROUP_ACCUMULATOR_PARAM_NAME }, required = true)
	protected abstract Integer transformDelegate(Integer counter, DataRecord groupAccumulator)
			throws ComponentNotReadyException, TransformException;

	@Override
	public int transformOnError(Exception exception, int counter, DataRecord inputRecord, DataRecord groupAccumulator,
			DataRecord[] outputRecords) throws TransformException {
		int result = 0;

		// both input and output records are accessible within the transform() function
		this.inputRecord = inputRecord;
		this.outputRecords = outputRecords;

		try {
			result = transformOnErrorDelegate(TransformUtils.getMessage(exception),
					ExceptionUtils.stackTraceToString(exception), counter, groupAccumulator);
		} catch (UnsupportedOperationException ex) {
			// no custom error handling implemented, throw an exception so the transformation fails
			throw new TransformException("Rollup failed!", exception);
		} catch (ComponentNotReadyException ex) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", ex);
		}

		// make both input and output records inaccessible again
		this.inputRecord = null;
		this.outputRecords = null;

		return result;
	}

	/**
	 * Called by {@link #transformOnError(Exception, int, DataRecord, DataRecord, DataRecord[])} to transform data
	 * records in a user-specific way defined in the CTL transform. May be overridden by the generated transform class.
	 * Throws <code>UnsupportedOperationException</code> by default.
	 *
	 * @param errorMessage an error message of the error message that occurred
	 * @param stackTrace a stack trace of the error message that occurred
     * @param counter the number of previous calls to this method for the current group update
     * @param groupAccumulator the data record used as an "accumulator" for the group or <code>null</code> if none
     * was requested
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = TRANSFORM_ON_ERROR_FUNCTION_NAME, parameterNames = {
			ERROR_MESSAGE_PARAM_NAME, STACK_TRACE_PARAM_NAME,
			COUNTER_PARAM_NAME, GROUP_ACCUMULATOR_PARAM_NAME }, required = false)
	protected Integer transformOnErrorDelegate(String errorMessage, String stackTrace, Integer counter,
			DataRecord groupAccumulator) throws ComponentNotReadyException, TransformException {
		throw new UnsupportedOperationException();
	}

	@Override
	protected final DataRecord getInputRecord(int index) {
		if (inputRecord == null) {
			throw new TransformLangExecutorRuntimeException(INPUT_RECORDS_NOT_ACCESSIBLE);
		}

		if (index != 0) {
			throw new TransformLangExecutorRuntimeException(new Object[] { index }, INPUT_RECORD_NOT_DEFINED);
		}

		return inputRecord;
	}

	@Override
	protected final DataRecord getOutputRecord(int index) {
		if (outputRecords == null) {
			throw new TransformLangExecutorRuntimeException(OUTPUT_RECORDS_NOT_ACCESSIBLE);
		}

		if (index < 0 || index >= outputRecords.length) {
			throw new TransformLangExecutorRuntimeException(new Object[] { index }, OUTPUT_RECORD_NOT_DEFINED);
		}

		return outputRecords[index];
	}

}
